using System.Linq;
using Microsoft.AspNetCore.Mvc;
using MvcDemo.Models;
using MvcDemo.Repository;
using MvcDemo.ResportParam;

namespace MvcDemo.Controllers
{
    public class ProductController : Controller{
        public IActionResult index(){
            var efr = new EfRepository<Product>();
            var pro =efr.Table.Where(x=>x.IsDeleted==false).ToList();
            return View(pro);
     
        }
        public IActionResult CreateOrUp(int id){
          var efr = new EfRepository<Product>();
            var stu = efr.Table.Where(x =>x.id==id).FirstOrDefault();
            return View(stu);
        }
        public IActionResult Save(ProResport pro){
            var efr=new EfRepository<Product>();
            if(pro.id>0){
                    var byid= efr.GetById(pro.id);
                    if (byid != null)
                    {
               byid.ProductName=pro.ProductName;
                byid.Supplier=pro.Supplier;
                byid.Price=pro.Price;
                byid.Stock=pro.Stock;
                byid.Remarks=pro.Remarks;
                efr.Update(byid);
                    }
            }else{
                efr.Insert(new Product{
                ProductName=pro.ProductName,
                Supplier=pro.Supplier,
                Price=pro.Price,
                Stock=pro.Stock,
                Remarks=pro.Remarks,
                Categoryid=1
                });   
            } return Ok();
        }
        public IActionResult Delect(int id){
            var efr=new EfRepository<Product>();
            efr.Delect(id);
            return RedirectToAction("index");
        }
    }
}